package val.to.uploader;

import java.util.ArrayList;

import val.to.UI.PhotoUploadQueue;
import val.to.entities.UploadItem;
import val.to.entities.UploadItem.UploadStatus;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class UploadManager {
	ArrayList<UploadItem> mUploadedItems;
	ItemStatusChangedListener mChangeListener;
	NotificationManager mNotificationManager;
	Notification mNotification;
	Context mAppContext;

	public UploadManager() {
		mUploadedItems = new ArrayList<UploadItem>();
	}

	public synchronized void Upload(Context context, UploadItem Item) {
		if (mAppContext == null)
			mAppContext = context.getApplicationContext();
		mUploadedItems.add(Item);
		QueueUpload(Item);
		ShowNotification(true);
	}

	private synchronized void QueueUpload(UploadItem Item) {
		Intent uploaderService = new Intent(mAppContext,
				PhotoUploadService.class);
		uploaderService.putExtra("itemid", Item.getID());
		mAppContext.startService(uploaderService);
		OnStatusChanged(Item);
	}

	// show notification only is upload queue activity isn't visible
	public void ShowNotification(boolean Start) {
		if (PhotoUploadQueue.IsVisible)
			return;
		CharSequence contentTitle = "Valto photos upload";
		if (mNotificationManager == null)
			mNotificationManager = (NotificationManager) mAppContext
					.getSystemService(Context.NOTIFICATION_SERVICE);
		int icon = val.to.R.drawable.valto_icon;

		String tickerText = "Valto Uploading " + getCompletedCount() + "/"
				+ getCount();

		long when = System.currentTimeMillis();
		mNotification = new Notification(icon, tickerText, when);

		mNotification.when = System.currentTimeMillis();
		Intent notificationIntent = new Intent(
				mAppContext.getApplicationContext(), PhotoUploadQueue.class);
		PendingIntent contentIntent = PendingIntent.getActivity(mAppContext, 0,
				notificationIntent, 0);
		CharSequence contentText = "Uploaded " + getCompletedCount() + "/"
				+ getCount();
		mNotification.setLatestEventInfo(mAppContext, contentTitle,
				contentText, contentIntent);
		mNotification.flags = Notification.FLAG_AUTO_CANCEL;

		mNotificationManager.notify(892342, mNotification);
	}

	public synchronized void DeleteItem(UploadItem Item) {
		mUploadedItems.remove(Item);
	}

	public synchronized void ReUploadItem(UploadItem Item) {
		Item.SetStatus(UploadStatus.Pending);
		Item.setErrorMessage(null);
		Item.setErrorDetails(null);
		QueueUpload(Item);
	}

	private synchronized UploadItem getItemByID(int ItemID) {
		for (UploadItem item : mUploadedItems) {
			if (item.getID() == ItemID)
				return item;
		}
		return null;
	}

	public synchronized UploadItem UploadStarted(int ItemID) {
		UploadItem item = getItemByID(ItemID);
		if (item == null)
			return null;
		item.SetStatus(UploadStatus.Uploading);
		OnStatusChanged(item);
		return item;
	}

	public synchronized void OnStatusChanged(UploadItem Item) {
		if (Item != null) {
			if (Item.getStatus() == UploadStatus.Uploaded)
				ShowNotification(false);
			if (mChangeListener != null)
				mChangeListener.onItemChanged();
		}
	}

	public synchronized int getCompletedCount() {
		int count = 0;
		for (UploadItem item : mUploadedItems) {
			if (item.getStatus() == UploadStatus.Uploaded)
				count++;
		}
		return count;
	}

	public synchronized void removeCompletedUploads() {
		for (int i = 0; i < mUploadedItems.size(); i++) {
			if (mUploadedItems.get(i).getStatus() == UploadStatus.Uploaded) {
				try {
					if (mUploadedItems.get(i).getThumbnail() != null)
						mUploadedItems.get(i).getThumbnail().recycle();
				} catch (Exception e) {
				}
				mUploadedItems.remove(i--);
			}
		}
	}

	public synchronized void removeAllUploads() {
		for (int i = 0; i < mUploadedItems.size(); i++) {
			if (mUploadedItems.get(i).getStatus() != UploadStatus.Uploading) {
				try {
					if (mUploadedItems.get(i).getThumbnail() != null)
						mUploadedItems.get(i).getThumbnail().recycle();
				} catch (Exception e) {
				}
				mUploadedItems.remove(i--);
			}
		}
	}

	public synchronized int getCount() {
		return mUploadedItems.size();
	}

	public synchronized UploadItem getItem(int position) {
		return mUploadedItems.get(position);
	}

	public synchronized void setOnStatusChangedListener(
			ItemStatusChangedListener Listener) {
		mChangeListener = Listener;
	}
}
